package com.nuo.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {
	private static SessionFactory sessionFactory;
	
	private static SessionFactory getSessionFactory() {
		if(sessionFactory == null) {
			try {
				String configFile = "/hibernate.cfg.xml";
				Configuration config = new Configuration();
				config.configure();
//				config.configure(configFile);
//    			config.addClass(com.test.bean.User.class);
				sessionFactory = config.buildSessionFactory();
			} catch (Exception e) {
				System.err.println("%%%% Error Creating SessionFactory %%%%");
				e.printStackTrace();
			}
		}
		return sessionFactory;	
	}
	
	public static Session getSession() {
		if(sessionFactory == null){
			getSessionFactory();
		}
		if(sessionFactory != null){
			return sessionFactory.openSession();
		}else{
			return null;
		}
	}
}